/**
 * 测试tRPC连接的工具函数
 */

import { trpc } from './trpc';

/**
 * 测试tRPC health接口
 */
export async function testTrpcHealth() {
  try {
    console.log('开始测试tRPC health接口...');
    const result = await trpc.health.check.query();
    console.log('tRPC调用成功:', result);
    return {
      success: true,
      data: result,
      message: 'tRPC调用成功',
      timestamp: new Date().toISOString(),
    };
  } catch (error) {
    console.error('tRPC调用失败:', error);
    return {
      success: false,
      error: error,
      message: 'tRPC调用失败',
      timestamp: new Date().toISOString(),
    };
  }
}

/**
 * 使用fetch直接测试tRPC接口
 */
export async function testTrpcWithFetch() {
  try {
    console.log('使用fetch测试tRPC接口...');
    const response = await fetch('http://localhost:3017/trpc/health.check', {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    const data = await response.json();
    console.log('Fetch调用成功:', data);
    return {
      success: true,
      data: data,
      message: 'Fetch调用成功',
      timestamp: new Date().toISOString(),
    };
  } catch (error) {
    console.error('Fetch调用失败:', error);
    return {
      success: false,
      error: error,
      message: 'Fetch调用失败',
      timestamp: new Date().toISOString(),
    };
  }
}
